﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Controls;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Windows;
using System.Collections.ObjectModel;
using WebServerConnectorDll;
using System.Windows.Input;

namespace FruitManagerApp
{
    public class PersonnelGroupViewModel : DataListViewModel
    {
        private UserControl gWindow;
        private ParentWindowViewModel gParentWindowViewModel;
        private PersonnelGroupModel gPersonnelGroupModel;

        public PersonnelGroupViewModel(UserControl pWindow, ParentWindowViewModel pParentWindowViewModel)
        {
            gWindow = pWindow;
            gParentWindowViewModel = pParentWindowViewModel;
            gPersonnelGroupModel = new PersonnelGroupModel();

            UpdateList();
        }

        public void UpdateList()
        {
            DataList.Clear();

            HttpResponse response = API.SelectPersonnelGroup();
            if(response.S == HttpResponse.Success)
            {
                List<CPersonnelGroup> list = JSON.parse<List<CPersonnelGroup>>(response.D);
                foreach(var item in list)
                {
                    DataList.Add(item);
                }
            }
        }

        public ObservableCollection<CPersonnelGroup> DataList
        {
            get
            {
                return gPersonnelGroupModel.DataList;
            }

            set
            {
                DataList = value;
                RaisePropertyChanged("DataList");
            }
        }

        #region Command
        protected override void AddCommand(object obj)
        {

        }

        protected override void CheckedCommand(object obj)
        {
            if(IsChecked)
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = true;
                }
            }
            else
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = false;
                }
            }
        }

        protected DelegateCommand<object> personnelGroupEditClick;
        public DelegateCommand<object> PersonnelGroupEditClick
        {
            get
            {
                return personnelGroupEditClick ?? (personnelGroupEditClick = new DelegateCommand<object>(ExcutePersonnelGroupEditClick));
            }
        }

        protected virtual void ExcutePersonnelGroupEditClick(object obj)
        {
            PersonnelGroupPermission window = new PersonnelGroupPermission();
            PersonnelGroupPermissionViewModel viewModel = new PersonnelGroupPermissionViewModel(window, this, (CPersonnelGroup)obj);
            window.DataContext = viewModel;
            window.ShowDialog();
        }
        #endregion
    }
}
